package com.fulihui.wgj.admin.api.wxtemplate;

import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.api.base.BaseController;
import com.fulihui.wgj.admin.api.wxtemplate.request.AutoLogQueryRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendTemplateQueryRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplateAutoTaskRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplateAutoTaskSaveRequest;
import com.fulihui.wgj.admin.api.wxtemplate.vo.TemplateAutoLogVO;
import com.fulihui.wgj.admin.api.wxtemplate.vo.TemplateAutoTaskVO;
import com.fulihui.wgj.admin.integration.AutoTaskServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: Levon
 * @date: 2018-08-07 15:04
 */
@RestController
@RequestMapping("/template/auto")
@Api(tags = "模板消息自动发送相关接口")
public class SendAutoTemplateController extends BaseController {

    @Autowired
    private AutoTaskServiceClient autoTaskServiceClient;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation("自动任务分页查询")
    public Result<PageView<TemplateAutoTaskVO>> taskListPage(@RequestBody @Valid TemplateAutoTaskRequest request, BindingResult validResult,
                                                             HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        PageView<TemplateAutoTaskVO> pageView = autoTaskServiceClient.querytask(request);
        return JsonResultBuilder.succ(pageView);
    }

    @RequestMapping(value = "/log", method = RequestMethod.POST)
    @ApiOperation("自动任务发送记录分页查询")
    public Result<PageView<TemplateAutoLogVO>> taskLogPage(@RequestBody @Valid AutoLogQueryRequest request, BindingResult validResult,
                                                           HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        PageView<TemplateAutoLogVO> pageView = autoTaskServiceClient.querySendLog(request);
        return JsonResultBuilder.succ(pageView);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiOperation("创建任务")
    public Result<Boolean> createTask(@RequestBody @Valid TemplateAutoTaskSaveRequest request, BindingResult validResult,
                                      HttpServletRequest servletRequest) {

        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return autoTaskServiceClient.createAutoTask(request);
    }

    @RequestMapping(value = "/count", method = RequestMethod.POST)
    @ApiOperation("计算预计发送人数")
    public Result<String> countSendTaskUser(@RequestBody @Valid TemplateAutoTaskSaveRequest request, BindingResult validResult,
                                            HttpServletRequest servletRequest) {

        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return autoTaskServiceClient.countUserAuto(request);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ApiOperation("自动任务详情查询查询")
    public Result<TemplateAutoTaskVO> taskListPage(@RequestBody @Valid SendTemplateQueryRequest request, BindingResult validResult,
                                                   HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        Result<TemplateAutoTaskVO> templateAutoTaskVOResult = autoTaskServiceClient.querytaskById(request);
        return templateAutoTaskVOResult;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation("修改任务")
    public Result<Boolean> editTask(@RequestBody @Valid TemplateAutoTaskSaveRequest request, BindingResult validResult,
                                    HttpServletRequest servletRequest) {

        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return autoTaskServiceClient.editAutoTask(request);
    }
}
